package com.java.mbti.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.java.mbti.service.MbtiService;
import com.java.mbti.vo.MbtiPointVO;
import com.java.mbti.vo.MbtiTypeVO;
import com.java.mbti.vo.PaperDTO;
import com.java.member.vo.MemberVO;

@Controller
public class MbtiController {

	@Autowired
	private MbtiService service;
	
	
	@RequestMapping(value={"/mbtiTest.do"}) 
	public ModelAndView mbtiTest(HttpSession session, HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		try {
			ArrayList<PaperDTO> list = service.mbtiTest();
			mv.addObject("test", list);
			mv.setViewName("mbtiTest.mbti");
		} catch (Exception e) {
			e.printStackTrace();
			mv.setViewName("index.main");
		}
		return mv;
	}
	
	@RequestMapping(value={"/marking.do"})
	public ModelAndView marking(HttpSession session,HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		MemberVO vo  = (MemberVO)session.getAttribute("login_info");
		
		ArrayList<String> list = new ArrayList<String>();
		
			for(int i = 1; i< 95; i++){
				list.add(request.getParameter(Integer.toString(i)));
			}
			
		try { 
			MbtiPointVO requestVO = service.markingMBTI(list,vo);
			if(requestVO != null){
				MbtiTypeVO typeVO = service.searchType(requestVO.getMbtiType());
				mv.addObject("vo", requestVO);
				mv.addObject("typeVO", typeVO);
				mv.setViewName("mbtiResult.mbti");
			}
		}catch(Exception e){
			mv.setViewName("memberTest.mem");
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping(value={"/mbtimail.do"})
	public ModelAndView mbtimail(HttpSession session, HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		MemberVO vo  = (MemberVO)session.getAttribute("login_info");
		vo.setName(vo.getMember_id());
		vo.setEmail(vo.getEmail());
		vo.setEmail_type(vo.getEmail_type());
		try {
			MbtiPointVO pvo = new MbtiPointVO();
			MbtiTypeVO typeVO = new MbtiTypeVO();
			
			pvo = service.resultSearchOne();
			
			typeVO = service.searchType(pvo.getMbtiType());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", vo.getName());
			map.put("email", vo.getEmail());
			map.put("email_type", vo.getEmail_type());
			map.put("mbtitype", typeVO.getType_name());
			map.put("type_kr", typeVO.getType_kr_nm());
			map.put("type_title", typeVO.getType_title());
			map.put("type_description", typeVO.getType_description());
			map.put("e", pvo.getePoint());
			map.put("i", pvo.getiPoint());
			map.put("s", pvo.getsPoint());
			map.put("n", pvo.getnPoint());
			map.put("t", pvo.gettPoint());
			map.put("f", pvo.getfPoint());
			map.put("j", pvo.getjPoint());
			map.put("p", pvo.getpPoint());
			service.sendMbtiMail(map);
			mv.setViewName("jsonView");
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch(Exception e){
			mv.setViewName("memberTest.mem");
			e.printStackTrace();
		}
		return mv;
	}
}

